
local h = 35
local w0 = 350
local h0 = h*4 + 50
local ui = class.panel:builder 
{
    x = 1920 - w0,
    y = 400,
    w = w0,
    h = h0,
    fdf = 'tooltip_backdrop0',
}
ui:add_text('排行榜',0,5,w0,25,12,5)
local panels = {}

local fiil = [[ReplaceableTextures\TeamColor\TeamColor00.blp]]
for a=1,4 do
    local panel = ui:add_panel([[core\bantouming.blp]],20,(a-1)*h+40,w0-40,h)
    panel.name = panel:add_text('玩家'..a,0,0,100,nil,12,5)
    panel.load = panel:add_panel(fiil,100,2.5,panel.w - 100,panel.h-5)
    panel.load.max_w = panel.load.w
    panel.dps = panel:add_text('999999',100,0,panel.w,h,12,4)
    panel.rate = panel:add_text(a*25,panel.w - 100,0,100,h,12,5)
    panel.name:set_color(0xffcccccc)
    panel.dps:set_color(0xcffffff00)
    panel.rate:set_color(0xcffffff00)
    panels[a] = panel
end

function ui:up_date(unit)
    local list = unit:get_data('伤害表') or {}
    local max = 0
    for a,panel in ipairs(panels) do
        local player = ac.player[a]
        local dps = list[a] or 0
        max = math.max(max,dps)
    end
    for a,panel in ipairs(panels) do
        local player = ac.player[a]
        local dps = list[a] or 0
        local rate1 = math.min(dps/unit:get('生命上限'),1)
        local rate2 = max>0 and math.min(dps/max,1) or 0
        panel.load:set_width(rate2*panel.load.max_w+0.01)
        panel.name:set_text(player:get_name())
        panel.rate:set_text(('%0.2f%%'):format(rate1*100))
        panel.dps:set_text(format_number1(dps))
    end
end

ui:hide()
ac.loop(30,function()
    local player = ac.player.self
    local time = player.show_dps or 0
    local unit = player.dps_unit
    if time>ac.clock() and unit then
        ui:up_date(unit)
        ui:show()
    else
        ui:hide()
    end
end)


ac.buff('排行榜'){
    on_add = function(self)
        local list = {}
        self.target:set_data('伤害表',list)
        self:gc(self.target:event '单位-受到伤害'(function(_,target,source,data)
            local player = source.owner
            list[player.id] = (list[player.id] or 0) + data.real_damage
            player.show_dps = ac.clock() + 5000
            player.dps_unit = self.target
        end))
    end
}
